package com.ewili.x.special.task.thread.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ewili.framework.utils.HttpClientUtils;
import com.ewili.framework.utils.ObjectHttpClientUtils;
import com.ewili.x.special.dao.inter.ExecsMarketDao;
import com.ewili.x.special.entity.ExecsMarket;
import com.ewili.x.special.task.thread.inter.ExecsMarketThreadTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ExecsMarketThreadTaskImpl implements ExecsMarketThreadTask {

    @Autowired
    private ExecsMarketDao execsMarketDao;

    //@Async("myTaskAsyncPool")
    @Override
    public void execsMarket(String doUrl) {
        String content = HttpClientUtils.getContent(doUrl, 3000000);
        JSONArray execsArr = ObjectHttpClientUtils.formatEastMoneyJson(content, "var HtHcMiDN=").getJSONObject("result").getJSONArray("data");
        for (int j = 0; j < execsArr.size(); j++) {
            JSONObject object = execsArr.getJSONObject(j);
            Date date = object.getDate("tdate");
            Long increaseCompanies = object.getLong("zcjs");
            Long decreaseCompanies = object.getLong("jcjs");
            Double increaseAmount = object.getDouble("zcgfje");
            Double decreaseAmount = object.getDouble("jcgfje");
            Double netCash = object.getDouble("gfbdjje");
            Long increaseStocks = object.getLong("zcgfsl");
            Long decreaseStocks = object.getLong("jcgfsl");
            Long netStocks = object.getLong("gfbdjsl");
            Double szzs = object.getDouble("NEW");
            Double changeRate = object.getDouble("CHG");

            boolean isExists = execsMarketDao.existsByDate(date);
            if (isExists || changeRate == null) {
                continue;
            }

            ExecsMarket execsMarket = new ExecsMarket();
            execsMarket.setDate(date);
            execsMarket.setIncreaseCompanies(increaseCompanies);
            execsMarket.setDecreaseCompanies(decreaseCompanies);
            execsMarket.setIncreaseAmount(increaseAmount);
            execsMarket.setDecreaseAmount(decreaseAmount);
            execsMarket.setNetCash(netCash);
            execsMarket.setIncreaseStocks(increaseStocks);
            execsMarket.setDecreaseStocks(decreaseStocks);
            execsMarket.setNetStocks(netStocks);
            execsMarket.setSzzs(szzs);
            execsMarket.setChangeRate(changeRate);

            execsMarketDao.saveAndFlush(execsMarket);

        }
    }
}
